/*
 * File:   CookingClock.cpp
 * Author: joao
 *
 * Created on November 29, 2010, 5:16 PM
 */

#include "CookingClock.h"


using namespace std;

CookingClock::CookingClock() {
    widget.setupUi(this);

    widget.alarmClock->setMaximum(60);
    widget.alarmClock->setMinimum(0);
    widget.alarmClock->setNotchesVisible(true);
    widget.alarmClock->setNotchTarget(1.0);
    widget.alarmClock->setValue(0);

    timer = new QTimer(this);
    music = NULL;

    connect(timer, SIGNAL(timeout()), this, SLOT(decrementMinutes()));
    connect(widget.startButton,SIGNAL(pressed()),this,SLOT(startClock()));
    connect(widget.stopButton,SIGNAL(pressed()),this,SLOT(stopClock()));

    
}

CookingClock::~CookingClock() {
}


void CookingClock::startClock() {


    int currentMinutes;

    currentMinutes = widget.alarmClock->value();

    if (currentMinutes != 0) {

        timer->start(60*1000);
        widget.startButton->setEnabled(false);
        

    }

}

void CookingClock::decrementMinutes() {

    int currentMinutes;

    currentMinutes = widget.alarmClock->value();

    if(currentMinutes == 1) {


        music = Phonon::createPlayer(Phonon::MusicCategory,Phonon::MediaSource(":/sounds/alarm_clock.wav"));
        music->play();


    }

    widget.alarmClock->setValue(currentMinutes-1);




    
}

void CookingClock::stopClock() {


    timer->stop();

    widget.startButton->setEnabled(true);


    if(music != NULL)
    {
        music->stop();
        music = NULL;
    }

   
}